﻿using GPSTrackingWebsiteManager.ClassManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class StockFileTypeOfFile : SessionManagerPage
    {
        private void GetAllStockFilesTypes()
        {
            StockFileTypeGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.StockFilesTypeOfFileProvider.GetAll();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                

                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.StockFilesTypeOfFileColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.StockFileTypeGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
            }
        }

        protected void StockFileTypeGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllStockFilesTypes();
            StockFileTypeGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
        }

        protected void StockFileTypeGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();            
            try
            {
                GPSTrackingDBV2.Entities.StockFilesTypeOfFile StockFilesTypeOfFileEntity = ClassManager.InvokeEntityManager.Invoke_StockFileTypeOfFile_Entity(e);
                StockFilesTypeOfFileEntity.StockFileTypeOfFileId = GPSTrackingDBV2Custom.Data.DataRepository.StockFilesTypeOfFileProvider.GetNextID();                

                //Log
                DateTime dateTransaction = DateTime.Now;
                Int64 tick = TickManager.TickManager.GetTick(dateTransaction);

                GPSTrackingDBV2.Entities.StockFilesTypeOfFileLog StockFilesTypeOfFileLogEntity = new GPSTrackingDBV2.Entities.StockFilesTypeOfFileLog();
                StockFilesTypeOfFileLogEntity.StockFileTypeOfFileId = StockFilesTypeOfFileEntity.StockFileTypeOfFileId;
                StockFilesTypeOfFileLogEntity.TransactionDate = dateTransaction;
                StockFilesTypeOfFileLogEntity.Tick = tick;
                StockFilesTypeOfFileLogEntity.SyncTransaction = SessionManager.SysnTransactionType_Insert;

                StockFilesTypeOfFileLogEntity.Name = StockFilesTypeOfFileEntity.Name;

                SessionManager objSessionManager = new SessionManager();
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
                Dictionary<string, string> AttribTransacion = objSessionManager.GetAttribTransaction();

                StockFilesTypeOfFileLogEntity.TypeUserTransaction = AttribTransacion[SessionManager.AttribTransaction.TypeUserTransaction.ToString()];
                StockFilesTypeOfFileLogEntity.UsernameTransaction = AttribTransacion[SessionManager.AttribTransaction.UsernameTransaction.ToString()]; ;
                StockFilesTypeOfFileLogEntity.CompanyIdTransaction = objSessionManager.CompanyIDSelectedComboBox;

                //Transaction
                tm.BeginTransaction();

                StockFilesTypeOfFileEntity.LastTick = tick;
                GPSTrackingDBV2.Data.DataRepository.StockFilesTypeOfFileProvider.Insert(StockFilesTypeOfFileEntity);

                    GPSTrackingDBV2.Data.DataRepository.StockFilesTypeOfFileLogProvider.Insert(tm, StockFilesTypeOfFileLogEntity);

                tm.Commit();                

            }
            catch (Exception ex)
            {
                tm.Rollback();

                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede insertar la información.");
            }
        }

        protected void StockFileTypeGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();      
            try
            {
                GPSTrackingDBV2.Entities.StockFilesTypeOfFile StockFilesTypeOfFileEntity = ClassManager.InvokeEntityManager.Invoke_StockFileTypeOfFile_Entity(e);
                StockFilesTypeOfFileEntity.OriginalStockFileTypeOfFileId = StockFilesTypeOfFileEntity.StockFileTypeOfFileId;
               

                //Log
                DateTime dateTransaction = DateTime.Now;
                Int64 tick = TickManager.TickManager.GetTick(dateTransaction);

                GPSTrackingDBV2.Entities.StockFilesTypeOfFileLog StockFilesTypeOfFileLogEntity = new GPSTrackingDBV2.Entities.StockFilesTypeOfFileLog();
                StockFilesTypeOfFileLogEntity.StockFileTypeOfFileId = StockFilesTypeOfFileEntity.StockFileTypeOfFileId;
                StockFilesTypeOfFileLogEntity.TransactionDate = dateTransaction;
                StockFilesTypeOfFileLogEntity.Tick = tick;
                StockFilesTypeOfFileLogEntity.SyncTransaction = SessionManager.SysnTransactionType_Update;

                StockFilesTypeOfFileLogEntity.Name = StockFilesTypeOfFileEntity.Name;

                SessionManager objSessionManager = new SessionManager();
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
                Dictionary<string, string> AttribTransacion = objSessionManager.GetAttribTransaction();

                StockFilesTypeOfFileLogEntity.TypeUserTransaction = AttribTransacion[SessionManager.AttribTransaction.TypeUserTransaction.ToString()];
                StockFilesTypeOfFileLogEntity.UsernameTransaction = AttribTransacion[SessionManager.AttribTransaction.UsernameTransaction.ToString()]; ;
                StockFilesTypeOfFileLogEntity.CompanyIdTransaction = objSessionManager.CompanyIDSelectedComboBox;

                //Transaction
                tm.BeginTransaction();

                    StockFilesTypeOfFileEntity.LastTick = tick;
                    GPSTrackingDBV2.Data.DataRepository.StockFilesTypeOfFileProvider.Update(StockFilesTypeOfFileEntity);

                    GPSTrackingDBV2.Data.DataRepository.StockFilesTypeOfFileLogProvider.Insert(tm, StockFilesTypeOfFileLogEntity);

                tm.Commit();


            }
            catch (Exception ex)
            {
                tm.Rollback();

                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede actualizar la información.");
            }
        }

        protected void StockFileTypeGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();      
            try
            {

                GPSTrackingDBV2.Entities.StockFilesTypeOfFile StockFilesTypeOfFileEntity = ClassManager.InvokeEntityManager.Invoke_StockFileTypeOfFile_Entity(e);

                //Log
                DateTime dateTransaction = DateTime.Now;
                Int64 tick = TickManager.TickManager.GetTick(dateTransaction);

                GPSTrackingDBV2.Entities.StockFilesTypeOfFileLog StockFilesTypeOfFileLogEntity = new GPSTrackingDBV2.Entities.StockFilesTypeOfFileLog();
                StockFilesTypeOfFileLogEntity.StockFileTypeOfFileId = StockFilesTypeOfFileEntity.StockFileTypeOfFileId;
                StockFilesTypeOfFileLogEntity.TransactionDate = dateTransaction;
                StockFilesTypeOfFileLogEntity.Tick = tick;
                StockFilesTypeOfFileLogEntity.SyncTransaction = SessionManager.SysnTransactionType_Delete;

                StockFilesTypeOfFileLogEntity.Name = StockFilesTypeOfFileEntity.Name;

                SessionManager objSessionManager = new SessionManager();
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
                Dictionary<string, string> AttribTransacion = objSessionManager.GetAttribTransaction();

                StockFilesTypeOfFileLogEntity.TypeUserTransaction = AttribTransacion[SessionManager.AttribTransaction.TypeUserTransaction.ToString()];
                StockFilesTypeOfFileLogEntity.UsernameTransaction = AttribTransacion[SessionManager.AttribTransaction.UsernameTransaction.ToString()]; ;
                StockFilesTypeOfFileLogEntity.CompanyIdTransaction = objSessionManager.CompanyIDSelectedComboBox;

                //Transaction
                tm.BeginTransaction();
                    StockFilesTypeOfFileEntity.LastTick = tick;
                    GPSTrackingDBV2.Data.DataRepository.StockFilesTypeOfFileProvider.Delete(StockFilesTypeOfFileEntity);

                    GPSTrackingDBV2.Data.DataRepository.StockFilesTypeOfFileLogProvider.Insert(tm, StockFilesTypeOfFileLogEntity);

                tm.Commit();
                
            }
            catch (Exception ex)
            {
                tm.Rollback();

                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede eliminar el registro porque el tipo de archivo porque se encuentra asociados a otros registros.");
            }
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            StockFileTypeGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in StockFileTypeGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }


            StockFileTypeGrid.MasterTableView.Rebind();
        }


        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Tipos ...');", true);
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }
    }
}